﻿var arrayHelper = function (key) {
    var list = new Array();
    //用于区分list的主键名称
    this.key = key;
    this.type = "object";
    this.Length = list.length;
    //根据ID查找
    this.Find = function (id) {
        var index = -1;
        if(this.type == "object"){
            for (var i = 0; i < list.length; i++) {
                if (list[i][this.key] == id) {
                    index = i;
                    break;
                }
            }
        }else{
            for (var i = 0; i < list.length; i++) {
                if (list[i] == id) {
                    index = i;
                    break;
                }
            }
        }
        return index;
    };
    //添加
    this.Add = function (obj) {
        if(typeof(obj) != "object"){
            this.type = "other";
        }
        
        var index = -1;
        if(this.type == "object"){
            index = this.Find(obj[this.key]);
            //如果ID已存在，则覆盖
            if (index > -1) {
                list[index] = obj;
            } else {
                list.push(obj);
            }
        }else{
            index = this.Find(obj);
            if (index > -1) {
                list[index] = obj;
            } else {
                list.push(obj);
            }
        }
        return index;
    };
    //移除
    this.Remove = function (id) {
        var index = this.Find(id);
        if (index > -1) {
            list.splice(index, 1);
        }
    };
    //移除所有
    this.RemoveAll = function () {
        list.splice(0, list.length);
    };
    //简单类型搜索直接传要搜索的值，如：FirstOrDefault(1)
    //复杂类型传Object，如：FirstOrDefault({ name: "John", age: 35 }})
    this.FirstOrDefault = function (sobj) {
        var obj;
        switch(this.type){
            case "number":
                obj = 0;
                break;
            case "string":
                obj = "";
                break;
            default:
                obj = null;
                break;
        }
        if(this.type == "object"){
            for (var i = 0; i < list.length; i++) {
                for(var s in sobj){
                    if(list[i][s] != sobj[s]){
                        obj = list[i];
                        break;
                    }
                }
            }
        } else {
            for (var i = 0; i < list.length; i++) {
                if (list[i] == sobj) {
                    obj = list[i];
                    break;
                }
            }
        }
        return obj;
    };
    //简单类型搜索直接传要搜索的值，如：Search(1)
    //复杂类型传Object，如：Search({ name: "John", age: 35 }})
    this.Search = function (sobj){
        var result = new Array();
        if(this.type != "object"){
            for (var i = 0; i < list.length; i++) {
                if (list[i] == sobj) {
                    result.push(list[i]);
                }
            }
        } else{
            for (var i = 0; i < list.length; i++) {
                var flag = true;
                for(var s in sobj){
                    if(list[i][s] != sobj[s]){
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    result.push(list[i]);
                }
            }
        }
        return result;
    };
    //简单类型只会返回0或1
    //复杂类型传Object，如：Count({ name: "John", age: 35 }})
    this.Count = function(cobj){
        if(cobj == undefined){
            return list.length;
        }
        
        var len = 0;
        if(this.type != "object"){
            var index = this.Find(cobj);
            if(index > -1){
                return 1;
            }
        }else{
            for (var i = 0; i < list.length; i++) {
                var flag = true;
                for(var s in cobj){
                    if(list[i][s] != cobj[s]){
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    len = len + 1;
                }
            }
        }
        return len;
    };
    //获取列表
    this.GetList = function () {
        return list;
    };
}